<?php
class Settings extends MY_Controller
{	
	var $curl = 'settings';
	var $name = '';
	var $search = '';
	var $per_page = 0;
	var $language;
	var $submenu = "_menu";
	var $parameter;
	var $id_parameter;
	
	function __construct()
	{
		parent::__construct();
				
		$this->load->model(array('/settings/settings_model'));

		$this->validation->set_fields($this->settings_model->fields);

		$this->data['Title'] = T_('Main parameters of HN Books');
		
		$this->language = parent::getLang();
		
		$this->name = T_('Main parameters of HN Books');
	}
	
	function index()
	{
		$this->data['list'] = $this->settings_model->get();
		
		$this->renderLayout();
	}
	
	function create()
	{		
		$this->data['Title'] .= ' / New parameter';
		
		if (count($_POST))
		{
			$rules = array('name', 'value', 'uom', 'description');
			$this->validation->set_rules($this->settings_model->getRules($rules));

			$this->validation->set_message('__nameUnique', '<span class="black bold">«'.T_("Parameter").'»</span> '.T_("already exists with such name. Please change the name."));
			
			if ($this->validation->run() === TRUE)
			{
				$item['name'] 			= $this->validation->name;
				$item['value'] 			= $this->validation->value;
				$item['uom'] 			= $this->validation->uom;		
				$item['description'] 	= $this->validation->description;
				
				if ($this->settings_model->insert($item))
				{
					redirect($this->curl .'/index');	
				}	
			}
		}

		$this->renderLayout();		
	}

	function edit($name = null)
	{
		if (!empty($name))
		{
			$object = $this->settings_model->get($name);

			$this->parameter = $object->name;
			$this->id_parameter = $object->id;
			
			$this->data['Title'] .= ' / ' . $object->name;
					
			if (!count($object))
			{
				$this->data['view'] = '_elements/query_no_object';
				$this->load->view($this->data['layout'], $this->data);
	
				return false; 
			}
		
			if (!count($_POST))
			{
				$this->validation->name 		= $object->name; 
				$this->validation->value 		= $object->value;
				$this->validation->uom 			= $object->uom;				
				$this->validation->description 	= $object->description;
			}
			else
			{
				$rules = array('name', 'value', 'description');
				$this->validation->set_rules($this->settings_model->getRules($rules));
	
				$this->validation->set_message('__nameUnique', '<span class="black bold">«'.T_("Parameter").'»</span> '.T_("already exists with such name. Please change the name."));

				if ($this->validation->run() === TRUE)
				{
					$item['name'] 		= $this->validation->name;
					$item['value'] 		= $this->validation->value;
					$item['uom'] 		= $this->validation->uom;
					$item['description']= $this->validation->description;

					$this->settings_model->update($item);
					
					redirect($this->curl .'/index');
				}
			}			
		}
		
		$this->renderLayout();
	}
		
	function deleteItems()
	{
		$data = $this->settings_model->delete($_REQUEST['items']);

		echo $data;
	}

	function delete($id)
	{
		$data = $this->settings_model->delete($id);

		if ($data)
		{
			redirect($this->curl . '/index');	
		}
		else
		{
			redirect($this->curl . '/edit/'. $id);
		}
	}
	
	function __nameUnique($name)
	{		
		return $this->settings_model->nameUnique($name);
	}
}
?>